<?php
class GlobalAction extends Action{
     protected function GlobalName(){
     	$user = A('User');
     	$user->checkuser();
     	$system = M('system');
     	$system_S = $system->findall();
     	$this->assign('title',$system_S[0]['title']);
     	$this->assign('keywords',$system_S[0]['keywords']);
     	$this->assign('description',$system_S[0]['description']);
     	$this->assign('images_savePath',$system_S[0]['images_savePath']);
		$this->assign('CURRENT',__CURRENT__);
		$this->assign('APP_PUBLIC_PATH',APP_PUBLIC_PATH.'/');
    }
    protected function get_alert($contents){
    	header("Content-type: text/html; charset=utf-8"); 
    	echo "<script type='text/javascript'>alert('$contents');</script>";
    }
		//以下为常用JS功能PHP封装
		protected function walert($msg,$status=true){
			echo '<script type=text/javascript>top.alert("'.$msg.'");</script>';
			$status?exit:"";
		}
		protected function Reload($window="window"){//窗口刷新
			echo '<script type=text/javascript>'.$window.'.location.reload();</script>';
		}
		protected function TWinReload(){
			$this->Reload();
		}
		protected function SelfClose(){//窗口关闭
			echo '<script type=text/javascript>window.close();</script>';
		}
		protected function Go2Url($url,$window='window'){//跳转及窗口的选择
			echo '<script type=text/javascript>'.$window.'.location.href="'.$url.'"</script>';
		}
		protected function TWinBack(){//回上一页
			echo '<script type=text/javascript>top.history.go(-1);</script>';
		}
		
		
	/*******************
	 * 
	 * ajax列表点击修改函数
	 *
	 ******************/
	protected function ajax_get($table,$field){
		$show = M($table);
    	$show->find($_POST['pid']);
    	if ($_POST['show_m'] == "1") {
    		$show->$field = '0';
			$show->save();
    		echo json_encode(array('show'=>'<font color="#FF0000">否</font>','value'=>'0'));
    	}else{
    		$show->$field = '1';
			$show->save();
    		echo json_encode(array('show'=>'是','value'=>'1'));
    	}
	}
    /*******************
	 * 
	 * 文件上传
	 *
	 ******************/
    protected function _upload($open) {//$open是否开启水印，1为是，0为否
    	$system = M('system');
    	$system = $system->findall();
        import("@.ORG.UploadFile");
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = $system[0]['images_size'];
        //设置上传文件类型
        $upload->allowExts = explode(',', $system[0]['images_sort']);
        //设置附件上传目录
        $upload->savePath = $system[0]['images_savePath'];
        //设置需要生成缩略图，仅对图像文件有效
        $upload->thumb = true;
        // 设置引用图片类库包路径
        $upload->imageClassPath = '@.ORG.Image';
        //设置需要生成缩略图的文件后缀
        $upload->thumbPrefix = 'm_,s_';  //生产2张缩略图
        //设置缩略图最大宽度
        $upload->thumbMaxWidth = $system[0]['images_maxwidth'].','.$system[0]['images_smallmaxwidth'];
        //设置缩略图最大高度
        $upload->thumbMaxHeight = $system[0]['images_maxheight'].','.$system[0]['images_smallmaxheight'];
        //设置上传文件规则
        $upload->saveRule = uniqid;
        //删除原图
        $upload->thumbRemoveOrigin = true;
        if (!$upload->upload()) {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        } else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
            $member = count($uploadList);
            if ($open == '1'){
		            import("@.ORG.Image");
		            //给m_缩略图添加水印, Image::water('原文件名','水印图片地址')
		        	if ($member == '1'){
						Image::water($uploadList[0]['savepath'].'m_'.$uploadList[0]['savename'], 'Public/Images/logo.png');
		            	return $uploadList[0]['savename'];
					}else{
						for ($i=0;$i<$member;$i++){
							Image::water($uploadList[$i]['savepath'].'m_'.$uploadList[$i]['savename'], 'Public/Images/logo.png');
						}
						return $uploadList;
					}
            }else{
            	if ($member == '1'){
            		return $uploadList[0]['savename'];
            	}else{
            		return $uploadList;
            	}
            }
			
            
        }
        /*******************
    	 * 多上传文件
    	 * $uploadList[$i][savepath] => ../Public/Uploads/
         * $uploadList[$i][savename] => 4db57daf13705.jpg
         * 大图m_ 缩略图s_
    	 *******************/
    }
    
    
    /*******************
	 * 
	 * 清空目录文件，如缓存等等
	 *
	 ******************/
	protected function del_dir($dirName){
		if ( $handle = opendir( "$dirName" ) ) {
		   while ( false !== ( $item = readdir( $handle ) ) ) {
			   if ( $item != "." && $item != ".." ) {
				   if ( is_dir( "$dirName/$item" ) ) {
				         $this->del_dir( "$dirName/$item" );
				   } else {
				   if( unlink( "$dirName/$item" ) )echo "成功删除文件： $dirName/$item<br />\n";
				   }
			   }
		   }
		   	closedir( $handle );
	  	}
	}
	/*******************
	 * 
	 * 编辑提交图片缩略图数据库连接
	 *
	 ******************/
    protected function cut_pic_s_url($pic){
    	$pic = explode('s_',$pic);
    	return $pic[1];
    }
	/*******************
	 * 
	 * 编辑提交图片大图数据库连接
	 *
	 ******************/
    protected function cut_pic_m_url($pic){
    	$pic = explode('m_',$pic);
    	return $pic[1];
    }
    
    
}
?>